package http

import (
	"bytes"
	"net/http"
)

// POST 请求
func POST(url string, header map[string]string, body []byte) (*http.Response, error) {
	payload := bytes.NewReader(body)
	client := &http.Client{}
	req, err := http.NewRequest(http.MethodPost, url, payload)
	if err != nil {
		return nil, err
	}
	for key, value := range header {
		req.Header.Add(key, value)
	}
	response, err := client.Do(req)
	defer response.Body.Close()
	return response, err
}

// GET 请求
func GET(url string, header map[string]string) (*http.Response, error) {
	client := &http.Client{}
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		return nil, err
	}
	for key, value := range header {
		req.Header.Add(key, value)
	}
	response, err := client.Do(req)
	defer response.Body.Close()
	return response, err
}

// ANY 请求
func ANY(url string, method string, header map[string]string, body []byte) (*http.Response, error) {
	client := &http.Client{}
	payload := bytes.NewReader(body)
	req, err := http.NewRequest(method, url, payload)
	if err != nil {
		return nil, err
	}
	for key, value := range header {
		req.Header.Add(key, value)
	}
	response, err := client.Do(req)
	defer response.Body.Close()
	return response, err
}
